<?php
/**
 * Notes:用户银行卡控制器
 * User: Administrator
 * Date: 2020/3/5
 * Time: 11:35
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace app\api\controller;

use app\api\model\Userbank as Ub;
use app\common\controller\Api;
use think\Db;
use think\Log;
use app\api\model\User as UserModel;
use app\api\model\Userbank as UserBankModel;
use app\api\model\Bank as BankModel;

class Userbank extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 我的余额
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //$id = $this->auth->id;
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        $res = [];
        $userBank = UserBankModel::findInfoByCondition(['user_id' => $userId, 'is_default' => 1]);
        //返回默认的
        $res['bank'] = $userBank ? $userBank : [];
        $res['money'] = Db::name('user')->where('id', $userId)->field('money')->find();
        $this->success('获取成功', $res);
    }

    /**
     * 我的添加银行卡
     */
    public function add()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        if ($userId <= 0) {
            $this->error('请重新登录', [],401);
        }

        $bankNum = $this->request->post('bank_num', '', 'trim');
        $bankName = $this->request->post('bank_name', '', 'trim');

        if (!$bankNum) {
            $this->error('银行卡号为必要信息，不能留空！');
        }
        if (!$bankName) {
            $this->error('银行名称为必要信息，不能留空！');
        }

        //判断卡号是否重复
        $userBank = UserBankModel::findInfoByCondition(['bank_num' => $bankNum]);
        if ($userBank) {
            $this->error('银行卡号重复，不能添加');
        }

        $data = [
            'user_id' => $userId,
            'bank_num' => $bankNum,
            'bank_name' => $bankName
        ];
        $bank = UserBankModel::addRecord($data);
        if ($bank) {
            $this->success('添加成功', []);
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 我的删除银行卡
     */
    public function del()
    {
        /*$data = $this->request->param();
        $id = $this->auth->id;
        $Ubs = new Ub;
        $res = $Ubs::where('user_id', $id)->where('id', $data['bank_num'])->delete();
        Log::info($res);
        if ($res !== 0 || $res !== false) {
            $this->success('删除成功', $res);
        }*/
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        $bankId = $this->request->post('bank_id', 0, 'intval');
        if ($bankId <= 0 || $userId <= 0) {
            $this->error('参数错误');
        }
        //获取银行卡的信息
        $userBank = UserBankModel::findInfoByCondition(['id' => $bankId]);
        if (!$userBank) {
            $this->error('参数错误');
        }
        if ($userBank['user_id'] != $userId) {
            $this->error('非法操作');
        }
        $res = UserBankModel::delRecord($userBank['id']);
        if ($res !== 0 || $res !== false) {
            $this->success('删除成功', []);
        } else {
            $this->success('删除失败', []);
        }
    }

    /**
     * 设置默认银行卡
     */
    public function setDefault()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        $bankId = $this->request->post('bank_id', 0, 'intval');
        if ($bankId <= 0 || $userId <= 0) {
            $this->error('参数错误');
        }
        //获取银行卡的信息
        $userBank = UserBankModel::findInfoByCondition(['id' => $bankId]);
        if (!$userBank) {
            $this->error('参数错误');
        }
        if ($userBank['user_id'] != $userId) {
            $this->error('非法操作');
        }
        //全部设置为非默认的
        UserBankModel::saveDataByCondition(['user_id' => $userId, 'is_default' => 1], ['is_default' => 0]);
        //该银行号设置为默认
        UserBankModel::saveData($bankId, ['is_default' => 1]);
        $this->success('设置成功', []);
    }

    /**
     * 我的银行卡列表
     */
    public function mylist()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        $res = [];
        $userBank = UserBankModel::findListByCondition(['user_id' => $userId]);
        if ($userBank) {
            foreach ($userBank as &$bakVal) {
                $bank = BankModel::findInfoByCondition(['bank_name' => $bakVal['bank_name']]);
                $bakVal['bank_icon'] = $bank ? $bank['bank_icon'] : '';
                $bakVal['bank_bg'] = $bank ? $bank['bank_bg'] : '';
            }
        }
        //返回默认的
        $res['bank_list'] = $userBank ? $userBank : [];
        $this->success('获取成功', $res);
    }

    /**
     * 显示添加银行卡页面
     */
    public function showadd()
    {
        $banks = BankModel::findListByCondition();
        $res['bank_list'] = $banks ? $banks : [];
        $this->success('获取成功', $res);
    }

}